#include <stdio.h>
#include <stdlib.h>

typedef enum { false, true } bool;
#define MaxVertexNum 10  /* maximum number of vertices */
typedef int Vertex;      /* vertices are numbered from 0 to MaxVertexNum-1 */

typedef struct AdjVNode* PtrToAdjVNode;
struct AdjVNode {
    Vertex AdjV;
    PtrToAdjVNode Next;
};

typedef struct Vnode {
    PtrToAdjVNode FirstEdge;
} AdjList[MaxVertexNum];

typedef struct GNode* PtrToGNode;
struct GNode {
    int Nv;
    int Ne;
    AdjList G;
};
typedef PtrToGNode LGraph;

LGraph ReadG(); /* details omitted */

void ShortestDist(LGraph Graph, int dist[], Vertex S);

int main()
{
    int dist[MaxVertexNum];
    Vertex S, V;
    LGraph G = ReadG();

    scanf("%d", &S);
    ShortestDist(G, dist, S);

    for (V = 0; V < G->Nv; V++)
        printf("%d ", dist[V]);

    return 0;
}
void ShortestDist(LGraph Graph, int dist[], Vertex S) {
    int queue[MaxVertexNum];
    int front = 0, rear = 0;
    Vertex v, w;
    int known[MaxVertexNum];
    int nv = Graph->Nv;
    PtrToAdjVNode helper;
    for (int i = 0; i < nv; i++) {
        known[i] = 0;
    }
    helper = Graph->G[S].FirstEdge;
    for (int i = 0; i < nv; i++) {
        if (i == S)
            dist[i] = 0;
        else
            dist[i] = -1;
    }
    queue[rear++] = S;
    while (front != rear) {
        v = queue[front++];
        known[v] = 1;
        helper = Graph->G[v].FirstEdge;
        while (helper) {
            if (dist[helper->AdjV] == -1) {
                dist[helper->AdjV] = dist[v] + 1;
                queue[rear++] = helper->AdjV;
            }
            helper = helper->Next;

        }
    }
}